package com.softeem.xyblogmaster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.xyblogmaster.domain.Articles;
import com.softeem.xyblogmaster.domain.Tags;
import com.softeem.xyblogmaster.jwt.JwtUtil;
import com.softeem.xyblogmaster.mapper.TagsMapper;
import com.softeem.xyblogmaster.service.ArticlesService;
import com.softeem.xyblogmaster.mapper.ArticlesMapper;
import com.softeem.xyblogmaster.util.PageInfo;
import com.softeem.xyblogmaster.util.ThreadContext;
import com.softeem.xyblogmaster.vo.ArticlesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 段伟韬的笔记本
* @description 针对表【blog_articles】的数据库操作Service实现
* @createDate 2025-04-15 16:31:04
*/
@Service
public class ArticlesServiceImpl extends ServiceImpl<ArticlesMapper, Articles>
    implements ArticlesService{

    @Autowired
    private TagsMapper tagsMapper;

    @Autowired
    private ArticlesMapper articlesMapper;

    @Autowired
    private JwtUtil jwtUtil;



    @Override
    public PageInfo page(int current,int size,String keyword)
    {
        LambdaQueryWrapper<Articles> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Articles::getTitle,"%"+keyword+"%");
        Page pages = this.page(new Page<>(current,size),lambdaQueryWrapper);
        System.out.println(pages.getRecords());
        PageInfo pageInfo = new PageInfo(pages,7);
        return pageInfo;
    }



    //多张表操作
    @Transactional
    public void saveAndRef(Articles articles, List<String> tagsList) {


        String token = ThreadContext.getItem();
        long uid = jwtUtil.getUid(token);
        articles.setAuthorId((int) uid);

        articlesMapper.insertOrUpdate(articles);

        if(tagsList!=null&&tagsList.size()>0){
            //在关联关系之前，先清空之前的关系
            tagsMapper.deleteRefByArticleId(articles.getId());
            //遍历所有的标签
            for(String name:tagsList){

                LambdaQueryWrapper<Tags> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(Tags::getName,name);
                Tags tags = tagsMapper.selectOne(lambdaQueryWrapper, false);
                if(tags==null){
                    //判断 如果标签在数据表中不存在  就先新增  再关联
                    Tags tags00=new Tags();
                    tags00.setName(name);
                    tagsMapper.insert(tags00);
                    Map refMap=new HashMap();
                    refMap.put("articleId",articles.getId());
                    refMap.put("tagId",tags00.getId());
                    articlesMapper.addRef(refMap);
                }
                else{
                    Map refMap=new HashMap();
                    //判断 如果标签在数据表中已经存在  就关联
                    refMap.put("articleId",articles.getId());
                    refMap.put("tagId",tags.getId());
                    articlesMapper.addRef(refMap);
                }
            }
        }

    }

    }

//    @Override
//    public ArticlesVO getArticlesWithTagsById(int aid) {
//        Articles articles = articlesMapper.selectArticleWithTagsById(aid);
//        ArticlesVO articlesVO = new ArticlesVO();
//        articlesVO.setTitle(articles.getTitle());
//        articlesVO.setContent(articles.getContent());
//        articlesVO.setCategoryId(articles.getCategoryId());
//        articlesVO.setTags(articles.getTags().stream().map(Tags::getName).toList());
//        return articlesVO;
//    }








